// pages/my/my.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    mylocation: '选择地址',
    longitude: null,
    latitude: null,
    markers: []
  },

  onLocation() {
    // 获取经纬度
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        console.log(res)
        // wx.openLocation({
        //   latitude: res.latitude,
        //   longitude: res.longitude,
        // })
        // 经纬度展示&选择
        wx.chooseLocation({
          latitude: res.latitude,
          longitude: res.longitude,
          success: (res) => {
            console.log(res)
            this.setData({
              mylocation: res.name
            })
          }
        })
      }
    })
  },
  onLogin() {
    // 登陆时，授权
    wx.getUserProfile({
      desc: '用于用户信息展示',
      success: (res) => {
        const {avatarUrl, nickName} =res.userInfo;
        wx.request({
          url: 'http://localhost:3000/api/register',
          method: 'POST',
          data: {
            avatarUrl,
            nickName
          },
          header: {
            token: wx.getStorageSync('token')
          },
          success: (res) => {
            console.log(res,'success')
            this.setData({
              avatarUrl: res.data.avatarUrl,
              nickName: res.data.nickName
            })
          }
        })
      }
    })
    
  },
  onConfirm(e) {
    const keyword = e.detail.value;
    wx.request({
      url: 'https://apis.map.qq.com/ws/place/v1/search',
      method: 'GET',
      data: {
        key: 'QHSBZ-WYFCL-SIEPY-MFVLT-4M266-2IFIW',
        keyword,
        boundary:`nearby(${this.data.latitude},${this.data.longitude},2000,1)`
      },
      success:(res) => {
        console.log(res)
        const data = res.data.data;
        const markers = data.map(item => {
          return {
            id: +item.id,
            latitude: item.location.lat,
            longitude: item.location.lng,
            iconPath: '/images/定位.png'
          }
        })
        this.setData({
          markers: markers,
          longitude: markers[0].longitude,
          latitude:markers[0].latitude,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 首次无感获取code，换取openid，返回token
    wx.login({
      success: (res) => {
        console.log(res)
        const code = res.code;
        wx.request({
          url: 'http://localhost:3000/api/login',
          method: 'POST',
          data: {
            code
          },
          success: (res) => {
            const {token, user} = res.data;
            wx.setStorageSync('token', token)
            if(user) {
              this.setData({
                userInfo: user
              })
            }
          }
        })
      },
    })

    wx.getLocation({
      type:'gcj02',
      success: (res) => {
        console.log(res)
        this.setData({
          longitude:res.longitude,
          latitude: res.latitude
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  toAddress() {
    wx.navigateTo({
      url: '',
    })
  }
})